<template>
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{article.title}}</span>
        <!-- 单张图片 -->
        <img alt="" v-if="article.cover.type === 1" class="thumb" v-lazy="article.cover.images[0]" />
      </div>
      <!-- 三张图片 -->
      <div class="thumb-box" v-if="article.cover.type > 1">
        <img v-for="(imgsrc, i) in article.cover.images" :key="i" alt="" class="thumb" v-lazy="imgsrc" />
      </div>
    </template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <span>{{article.aut_name}}&nbsp;&nbsp;{{article.comm_count}}评论&nbsp;&nbsp;{{dateFormat(article.pubdate)}}</span>
        <!-- 关闭按钮 -->
        <van-icon name="cross" @click.stop="show = true" />
      </div>
    </template>
  </van-cell>

  <!-- 反馈的动作面板 -->
  <van-action-sheet v-model:show="show" teleport="body" :closeable="false" cancel-text="取消" @closed="isShowFirst = true">
    <div v-if="isShowFirst">
      <van-cell :title="item.name" v-for="(item, index) in actions1" :key="index" title-class="title-center" clickable @click="onCellClick(item)" />
    </div>
    <div v-else>
      <van-cell title="返回" title-class="title-center" clickable @click="isShowFirst = true" />
      <van-cell :title="item.label" v-for="(item, index) in reports" :key="index" title-class="title-center" clickable @click="onFeedback(item.value)" />
    </div>
  </van-action-sheet>
</template>

<script>
import { ref } from 'vue'
import { dislikeArticle, reportArticle } from '@/api/homeAPI'
import reports from '@/api/constant'

export default {
  name: 'ArticleItem',
  props: {
    article: {
      type: Object,
      required: true
    }
  },
  emits: ['remove-article'],
  setup(props, { emit }) {
    // 控制动作面板的展示与隐藏
    const show = ref(false)
    // 是否展示第一个可选项的面板
    const isShowFirst = ref(true)
    // 文章的 Id
    const artId = props.article.art_id.toString()

    // 第一个面板的可选项列表
    const actions1 = [{ name: '不感兴趣' }, { name: '反馈垃圾内容' }, { name: '拉黑作者' }]

    // 选项的点击事件处理函数
    const onCellClick = async item => {
      if (item.name === '不感兴趣') {
        const { data: res } = await dislikeArticle(artId)
        if (res.message === 'OK') {
          // 触发自定义事件，并把文章 Id 发送给使用者
          emit('remove-article', artId)
        }
        show.value = false
      } else if (item.name === '反馈垃圾内容') {
        isShowFirst.value = false
      } else if (item.name === '拉黑作者') {
        console.log('TODO：拉黑作者')
        show.value = false
      }
    }

    // 点击了反馈的选项
    const onFeedback = async type => {
      const { data: res } = await reportArticle(artId, type)
      if (res.message === 'OK') {
        emit('remove-article', artId)
        show.value = false
      }
    }

    return {
      show,
      isShowFirst,
      actions1,
      reports,
      onCellClick,
      onFeedback
    }
  }
}
</script>

<style lang="less" scoped>
.article-info-container {
  border-top: 1px solid #f8f8f8;
}

.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.thumb-box {
  display: flex;
  justify-content: space-between;
}

/deep/ .title-center {
  text-align: center !important;
}
</style>
